/**
 * 时时彩的规则(基类)
 */
var sscRule = function() {
    /* 投注名称与服务器名称的对应关系
     "总和大" ：{
        display: "总和 - 大",
     }
    */
    this.map = {

    };
    var arr = ['大', '小', '单', '双'];
    for (var i in arr) {
        this.map['和' + arr[i]] = {
            display: '总和 - ' + arr[i],
            rate: 1.99
        }
    }
    var arr = ['龙', '虎', '和'];
    for (var i in arr) {
        this.map[arr[i]] = {
            display: arr[i],
            rate: arr[i] == '和' ? 9 : 1.99,
        }
    }

    var arr = ['大', '小', '单', '双', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    var arr2 = ['第一球', '第二球', '第三球', '第四球', '第五球'];
    var arr3 = ['一', '二', '三', '四', '五'];
    for (var i in arr2) {
        for (var j in arr) {
            this.map[arr3[i] + arr[j]] = {
                display: arr2[i] + " - " + arr[j],
                rate: j <= 3 ? 1.99 : 9.9,
            }
        }
    }

    var arr = ['前三', '中三', '后三'];
    var arr2 = ['豹子', '顺子', '对子', '半顺', '杂六'];
    var arr3 = [75, 14.5, 3.3, 2.5, 3];
    for (var i in arr) {
        for (var j in arr2) {
            this.map[arr[i][0] + arr2[j][0]] = {
                display: arr[i] + " - " + arr2[j],
                rate: arr3[j]
            }
        }
    }
};
sscRule.prototype = {};
sscRule.prototype.construct = sscRule;

sscRule.prototype.isOdd = function(v) {
    return v % 2 == 1;
};
sscRule.prototype.isEven = function(v) {
    return v % 2 == 0;
}
sscRule.prototype.isBig = function(v) {
    return v >= 5;
}
sscRule.prototype.isSmall = function(v) {
    return v < 5;
}
sscRule.prototype.isLong = function(list) {
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[4]);
    return a1 > a2;
}
sscRule.prototype.isHu = function(list) {
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[4]);
    return a1 < a2;
}
sscRule.prototype.isHe = function(list) {
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[4]);
    return a1 == a2;
}
sscRule.prototype.isHeOdd = function(list) {
    var total = this.he(list);
    return this.isOdd(total);
}
sscRule.prototype.isHeBig = function(list) {
    var total = this.he(list);
    return total >= 23;
}
sscRule.prototype.isQian3BaoZi = function(list) {
    return parseInt(list[0]) == parseInt(list[1]) && parseInt(list[0]) == parseInt(list[2]);
}
sscRule.prototype.isQian3ShunZi = function(list) {
    var arr = [parseInt(list[0]), parseInt(list[1]), parseInt(list[2])];
    arr.sort();
    if (arr[1] - arr[0] == 1 && arr[2] - arr[0] == 2) return true;
    if (arr[0] == 0 && arr[1] == 1 && arr[2] == 9) return true;
    if (arr[0] == 0 && arr[1] == 8 && arr[2] == 9) return true;
    return false;
}
sscRule.prototype.isQian3DuiZi = function(list) {
    if (this.isQian3BaoZi(list)) return false;
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[1]),
        a3 = parseInt(list[2]);
    return a1 == a2 || a1 == a3 || a2 == a3;
}
sscRule.prototype.isQian3BanShun = function(list) {
    if (this.isQian3ShunZi(list) || this.isQian3DuiZi(list)) return false;
    var a1 = parseInt(list[0]),
        a2 = parseInt(list[1]),
        a3 = parseInt(list[2]);
    var arr = [a1, a2, a3];
    arr.sort();
    if (arr[1] - arr[0] == 1 || arr[2] - arr[1] == 1) return true;
    if (arr[0] == 0 && arr[2] == 9) return true;
    return false;
}
sscRule.prototype.isQian3ZaLiu = function(list) {
    if (this.isQian3BaoZi(list) ||
        this.isQian3ShunZi(list) ||
        this.isQian3DuiZi(list) ||
        this.isQian3BanShun(list)) return false;
    return true;
}

sscRule.prototype.isZhong3BaoZi = function(list) {
    return parseInt(list[1]) == parseInt(list[2]) && parseInt(list[1]) == parseInt(list[3]);
}
sscRule.prototype.isZhong3ShunZi = function(list) {
    var arr = [parseInt(list[1]), parseInt(list[2]), parseInt(list[3])];
    arr.sort();
    if (arr[1] - arr[0] == 1 && arr[2] - arr[0] == 2) return true;
    if (arr[0] == 0 && arr[1] == 1 && arr[2] == 9) return true;
    if (arr[0] == 0 && arr[1] == 8 && arr[2] == 9) return true;
    return false;
}
sscRule.prototype.isZhong3DuiZi = function(list) {
    if (this.isZhong3BaoZi(list)) return false;
    var a1 = parseInt(list[1]),
        a2 = parseInt(list[2]),
        a3 = parseInt(list[3]);
    return a1 == a2 || a1 == a3 || a2 == a3;
}
sscRule.prototype.isZhong3BanShun = function(list) {
    if (this.isZhong3ShunZi(list) || this.isZhong3DuiZi(list)) return false;
    var a1 = parseInt(list[1]),
        a2 = parseInt(list[2]),
        a3 = parseInt(list[3]);
    var arr = [a1, a2, a3];
    arr.sort();
    if (arr[1] - arr[0] == 1 || arr[2] - arr[1] == 1) return true;
    if (arr[0] == 0 && arr[2] == 9) return true;
    return false;
}
sscRule.prototype.isZhong3ZaLiu = function(list) {
    if (this.isZhong3BaoZi(list) ||
        this.isZhong3ShunZi(list) ||
        this.isZhong3DuiZi(list) ||
        this.isZhong3BanShun(list)) return false;
    return true;
}

sscRule.prototype.isHou3BaoZi = function(list) {
    return parseInt(list[2]) == parseInt(list[3]) && parseInt(list[2]) == parseInt(list[4]);
}
sscRule.prototype.isHou3ShunZi = function(list) {
    var arr = [parseInt(list[2]), parseInt(list[3]), parseInt(list[4])];
    arr.sort();
    if (arr[1] - arr[0] == 1 && arr[2] - arr[0] == 2) return true;
    if (arr[0] == 0 && arr[1] == 1 && arr[2] == 9) return true;
    if (arr[0] == 0 && arr[1] == 8 && arr[2] == 9) return true;
    return false;
}
sscRule.prototype.isHou3DuiZi = function(list) {
    if (this.isHou3BaoZi(list)) return false;
    var a1 = parseInt(list[2]),
        a2 = parseInt(list[3]),
        a3 = parseInt(list[4]);
    return a1 == a2 || a1 == a3 || a2 == a3;
}
sscRule.prototype.isHou3BanShun = function(list) {
    if (this.isHou3ShunZi(list) || this.isHou3DuiZi(list)) return false;
    var a1 = parseInt(list[2]),
        a2 = parseInt(list[3]),
        a3 = parseInt(list[4]);
    var arr = [a1, a2, a3];
    arr.sort();
    if (arr[1] - arr[0] == 1 || arr[2] - arr[1] == 1) return true;
    if (arr[0] == 0 && arr[2] == 9) return true;
    return false;
}
sscRule.prototype.isHou3ZaLiu = function(list) {
    if (this.isHou3BaoZi(list) ||
        this.isHou3ShunZi(list) ||
        this.isHou3DuiZi(list) ||
        this.isHou3BanShun(list)) return false;
    return true;
}

// 计算总和
sscRule.prototype.he = function(list) {
    var total = 0;
    for (var i in list) {
        total += parseInt(list[i]);
    }
    return total;
}

/**
 * 计算露珠
 */
sscRule.prototype.luzhu = function(list, qiu, key) {
    // 将不是今天的开奖过滤掉
    // 比较特殊：已凌晨6点作为分割线
    var list2 = [];
    var today = new Date().Format("yyyy-MM-dd");
    for (var i in list) {
        if (list[i].time.startsWith(today)) list2.push(list[i]);
    }

    var self = this;
    if (key == 'count') {
        // 各数字的开奖次数
        var m = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            m[n]++;
        }
        return m;
    }
    if (key == 'number') {
        // 号码排盘(最多25列)
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "daxiao") {
        // 大小
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = self.isBig(n) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "danshuang") {
        // 单双
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = n % 2 == 0 ? "双" : "单";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "hedaxiao") {
        // 总和大小
        var m = [];
        for (var i in list2) {
            var n = self.isHeBig(list2[i].red) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "hedanshuang") {
        // 总和单双
        var m = [];
        for (var i in list2) {
            var n = self.isHeOdd(list2[i].red) ? "单" : "双";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "longhuhe") {
        // 龙虎和
        var m = [];
        for (var i in list2) {
            var n = '和';
            if (self.isLong(list[i].red)) n = '龙';
            else if (self.isHu(list[i].red)) n = '虎';
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    return [];
}

/**
 * 计算长龙信息
 */
sscRule.prototype.long = function(list) {
    var self = this;
    var r = [];
    if (isEmpty(list)) return r;

    // 总和: 大、小、单、双，龙 虎 和
    var l0 = list[0].red;

    var f = function(k, cb) {
        var t = 1;
        for (var i = 1; i < list.length; i++) {
            var l = list[i].red;
            if (!cb(l)) break;
            t++;
        }
        var oo = {}; oo[k] = t;
        if (t > 2) r.push(oo);
    };

    if (self.isHeBig(l0)) {
        f("总和 - 大", function(l) {
            return self.isHeBig(l);
        });
    }
    else {
        f("总和 - 小", function(l) {
            return !self.isHeBig(l);
        });
    }
    if (self.isHeOdd(l0)) {
        f("总和 - 单", function(l) {
            return self.isHeOdd(l);
        });
    }
    else {
        f("总和 - 双", function(l) {
            return !self.isHeOdd(l);
        });
    }
    if (self.isLong(l0)) {
        f("龙", function(l) {
            return self.isLong(l);
        });
    }
    else if (self.isHu(l0)) {
        f("虎", function(l) {
            return self.isHu(l);
        });
    }
    else if (self.isHe(l0)) {
        f("和", function(l) {
            return self.isHe(l);
        });
    }

    // 前三：豹子、顺子、对子、半顺、杂六
    if (self.isQian3BaoZi(l0)) {
        f("前三 - 豹子", function(l) {
            return self.isQian3BaoZi(l);
        });
    }
    else if (self.isQian3ShunZi(l0)) {
        f("前三 - 顺子", function(l) {
            return self.isQian3ShunZi(l);
        });
    }
    else if (self.isQian3DuiZi(l0)) {
        f("前三 - 对子", function(l) {
            return self.isQian3DuiZi(l);
        });
    }
    else if (self.isQian3BanShun(l0)) {
        f("前三 - 半顺", function(l) {
            return self.isQian3BanShun(l);
        });
    }
    else if (self.isQian3ZaLiu(l0)) {
        f("前三 - 杂六", function(l) {
            return self.isQian3ZaLiu(l);
        });
    }

    // 中三：豹子、顺子、对子、半顺、杂六
    if (self.isZhong3BaoZi(l0)) {
        f("中三 - 豹子", function(l) {
            return self.isZhong3BaoZi(l);
        });
    }
    else if (self.isZhong3ShunZi(l0)) {
        f("中三 - 顺子", function(l) {
            return self.isZhong3ShunZi(l);
        });
    }
    else if (self.isZhong3DuiZi(l0)) {
        f("中三 - 对子", function(l) {
            return self.isZhong3DuiZi(l);
        });
    }
    else if (self.isZhong3BanShun(l0)) {
        f("中三 - 半顺", function(l) {
            return self.isZhong3BanShun(l);
        });
    }
    else if (self.isZhong3ZaLiu(l0)) {
        f("中三 - 杂六", function(l) {
            return self.isZhong3ZaLiu(l);
        });
    }

    // 后三：豹子、顺子、对子、半顺、杂六
    if (self.isHou3BaoZi(l0)) {
        f("后三 - 豹子", function(l) {
            return self.isHou3BaoZi(l);
        });
    }
    else if (self.isHou3ShunZi(l0)) {
        f("后三 - 顺子", function(l) {
            return self.isHou3ShunZi(l);
        });
    }
    else if (self.isHou3DuiZi(l0)) {
        f("后三 - 对子", function(l) {
            return self.isHou3DuiZi(l);
        });
    }
    else if (self.isHou3BanShun(l0)) {
        f("后三 - 半顺", function(l) {
            return self.isHou3BanShun(l);
        });
    }
    else if (self.isHou3ZaLiu(l0)) {
        f("后三 - 杂六", function(l) {
            return self.isHou3ZaLiu(l);
        });
    }

    // 1-5球大小、单双
    var arr = ["第一球", "第二球", "第三球", "第四球", "第五球"];
    for (var index = 0; index < 5; index++) {
        var n = parseInt(l0[index]);
        if (self.isBig(n)) {
            f(arr[index] + " - 大", function(l) {
                return self.isBig(parseInt(l[index]));
            });
        }
        else {
            f(arr[index] + " - 小", function(l) {
                return !self.isBig(parseInt(l[index]));
            });
        }
        if (self.isOdd(n)) {
            f(arr[index] + " - 单", function(l) {
                return self.isOdd(parseInt(l[index]));
            });
        }
        else {
            f(arr[index] + " - 双", function(l) {
                return !self.isOdd(parseInt(l[index]));
            });
        }
    }

    // 排序
    r.sort(function(a, b) {
        var v1, v2;
        for (var k in a) v1 = a[k];
        for (var k in b) v2 = b[k];
        return v2 - v1;
    });
    return r;
}

/**
 * 转换显示的名称
 */
sscRule.prototype.getTargetName = function(name) {
    if (!this.map[name]) {
        console.error("错误的参数", name);
        return name;
    }
    return this.map[name].display;
};
